<template>
  <div class="container">
    <div class="main">
      <!-- 头部表单 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="学科名称">
          <el-input v-model="formInline.name"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clearForm">清楚</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="select" >查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" class="el-icon-edit" @click="addClass"
            >新增学科</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 分割线 -->
      <div class="separate">
        <p><span class="el-icon-info"></span> 共{{ total }}条记录</p>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%" >
        <el-table-column fixed prop="id" label="序号" width="100">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科" width="100">
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称" width="150">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="150">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="200">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="changestates(scope.row.id)"
              type="text"
              size="small"
            >
              {{scope.row.state===0?'启用':'禁用'}}
            </el-button>
            <el-button
            :disabled="scope.row.state===0"
              @click.native.prevent="changetags(scope.row.id)"
              type="text"
              size="small"
            >
              修改
            </el-button>
            <el-button
            :disabled="scope.row.state===0"
              @click.native.prevent="open(scope.row)"
              type="text"
              size="small"
            >
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
  <tagsadd :dialogVisible.sync="dialogVisible" :addInfo.sync="addInfo" :type="type" @renderlist="getRenderList"></tagsadd>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags.js'
import tagsadd from '@/module-hmmm/components/tags-add'
export default {
  components: { tagsadd },
  data () {
    return {
      type: '',
      dialogVisible: false,
      // fenye
      currentPage4: 1,
      info: {
        page: 1,
        pagesize: 10,
        keyword: '',
        disabled: null
      },
      total: 2,
      formInline: {
        name: '',
        state: null
      },
      tableData: [
        {
          id: '',
          tagName: '',
          creatorID: '',
          addData: '',
          subjectID: '',
          subjectName: '',
          totals: '',
          username: '',
          state: null
        }
      ],
      addInfo: {
        id: null,
        subjectName: '',
        tagName: '',
        subjectID: ''
      }
    }
  },
  methods: {
    // 页数，条数
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.info.pagesize = val
      this.info.page = 1
      this.getRenderList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.info.page = val
      this.getRenderList()
    },
    async getRenderList () {
      const { data } = await list()
      this.total = data.counts
      this.tableData = data.items
    },
    select () {
      if (this.formInline.state) {
        this.tableData = this.tableData.filter(item => item.state === Number(this.formInline.state))
      }
      if (this.formInline.name) {
        this.tableData = this.tableData.filter(item => item.tagName === this.formInline.name)
      }
    },
    clearForm () {
      this.formInline.name = ''
      this.formInline.state = null
      this.getRenderList()
    },
    addClass () {
      this.type = 'add'
      this.dialogVisible = true
    },
    open (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const tag = this.tableData.filter(item => item.id === row.id)
        await remove(tag[0])
        this.getRenderList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async changestates (id) {
      const tag = this.tableData.filter(item => item.id === id)
      await changeState(tag[0])
      this.tableData = this.tableData.map(item => {
        if (item.id === id) {
          item.state = item.state === 0 ? 1 : 0
          return item
        }
        return item
      })
    },
    changetags (id) {
      this.type = 'change'
      this.dialogVisible = true
      const tag = this.tableData.filter(item => item.id === id)
      this.addInfo.id = id
      this.addInfo.subjectName = tag[0].subjectName
      this.addInfo.tagName = tag[0].tagName
      this.addInfo.subjectID = tag[0].subjectID
    }
  },
  created () {
    this.getRenderList()
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
  .main {
    padding: 20px;
    background-color: #fff;
  }
  .separate {
    margin: 20px 0;
    width: 100%;
    height: 30px;
    background-color: #ccc;
    padding-left: 15px;
    border-radius: 5px;
    p {
      font-size: 18px;
      line-height: 30px;
    }
  }
}
</style>
